# 转码
# utf-8中文转char数组
# char数组转中文utf8

# -*- coding:utf-8 -*-
import tkinter 
from tkinter import filedialog
from tkinter import ttk 
import sys
import os



# 界面
top = tkinter.Tk()
# top.update()  #一定要刷新界面，否则打印出的值是1
top.title('UTF-8转码')
top.geometry('750x300+500+300') 
top.configure(bg='#EDF7FE')

utf8_str = tkinter.StringVar()
str_utf8 = tkinter.StringVar()

utf8_str1 = tkinter.StringVar()
str_utf81 = tkinter.StringVar()


zLabel = tkinter.Label(top,text='输入字符串:',font=('雅黑',12),padx=2,pady=2)
zLabel.pack()
zLabel.place(x=10,y=20)

entry = tkinter.Entry(top,width=75,textvariable=utf8_str)
entry.pack()
entry.place(x=124,y=20)
# entry_var.set('我是一个Entry')


zLabel = tkinter.Label(top,text='UTF-8字符串:',font=('雅黑',12),padx=2,pady=2)
zLabel.pack()
zLabel.place(x=10,y=50)

# zLabel = tkinter.Entry(top,width=60,textvariable=utf8_str1,font=('雅黑',12), justify='left')
_zLabel = tkinter.Text(top,bg='Seashell',font=('雅黑',12),wrap='char')
_zLabel.pack()
# _zLabel['state'] = 'readonly' 
_zLabel.place(x=120,y=50,width=580, height=60)

# 按钮
def zbtn_call():
	if len(utf8_str.get()) > 0:
		us = utf8_str.get().encode('utf8')
		# print('us:::',us)
		us = str(us)
		us = us[:-1]
		us = us[2:]
		# utf8_str1.set(us)
		_zLabel.delete('1.0','end')
		_zLabel.insert("insert",us)
zbtn = tkinter.Button(top,text='转 码',bg='#37A1EE',font=('雅黑',12),command=zbtn_call)
zbtn.pack()
zbtn.place(x=680,y=16,width=60, height=30)


zLabel = tkinter.Label(top,text='UTF-8字符串:',font=('雅黑',12),padx=2,pady=2)
zLabel.pack()
zLabel.place(x=10,y=120)

entry = tkinter.Entry(top,width=75,textvariable=str_utf8)
entry.pack()
entry.place(x=124,y=122)
# entry_var.set('我是一个Entry')


zLabel = tkinter.Label(top,text='字符串:',font=('雅黑',12),padx=2,pady=2)
zLabel.pack()
zLabel.place(x=10,y=180)

# zLabel = tkinter.Entry(top,width=60,textvariable=str_utf81,font=('雅黑',12))
# zLabel.pack()
# zLabel['state'] = 'readonly' 
# zLabel.place(x=120,y=122)

_zLabel1 = tkinter.Text(top,bg='Seashell',font=('雅黑',12),wrap='char')
_zLabel1.pack()
_zLabel1.place(x=120,y=180,width=580, height=60)

# 按钮
def zbtn_call():
	if len(str_utf8.get()) > 0:
		us = str_utf8.get()
		# print('us1:::',us)
		lus = us.split('\\x')
		# print(lus)
		bus = bytes()
		for k in lus:
			# print('k ',k)
			if len(k) > 0:
				bus = bus + bytes.fromhex(k[0:2])
				if len(k) > 2:
					bus = bus + k[2:].encode('utf8')
				# print(bus)
		us = bus.decode()
		# str_utf81.set(us)
		_zLabel1.delete('1.0','end')
		_zLabel1.insert("insert",us)

zbtn = tkinter.Button(top,text='还 原',bg='#37A1EE',font=('雅黑',12),command=zbtn_call)
zbtn.pack()
zbtn.place(x=680,y=118,width=60, height=30)

zLabel = tkinter.Label(top,text='By 大菠萝 制作',font=('雅黑',12),padx=2,pady=2)
zLabel.pack()
zLabel.place(x=600,y=255)

# 进入消息循环
top.mainloop()

